<?php  if (!defined('BASEPATH')) exit(header("Location: /"));
/**
 * Common Library
 *
 * @package		 CodeIgniter
 * @subpackage Library
 * @category	 Commonly Use Function Library
 *
 * @author		 detanto / detanto[at]gmail.com
 * @project		 pa hedy 01
 *
 * Modified 2009 10 11
 */
 class Common {

  var $langu;
  var $menu_id;
  var $segment2;
  var $segment3;
  var $segment4;

  // ------------------------------------------------------------------------
	/**
	 * Session Constructor
	 */
  function Common() {

    define('BASE_URL', base_url());
    $this->CI =& get_instance();

    /**
     *  Get URI segment to determine current page
     */
    $message  = $this->CI->session->flashdata('msgs');
    $username = $this->CI->session->userdata('USR_LOGINS');
    $this->CI->load->library(array('validation'));
    $this->CI->load->helper('form');
    $data['message']  = (empty($message)) ? '' : $message;
    $data['username'] = $username;
    $arrMenu0 = $this->CI->session->userdata('menuapp1');
    if (empty($arrMenu0) || !is_array($arrMenu0))
    {
        $arrMenu0 = $this->CI->crud->get_menu('20080227', $username,0);
        $arrMenu1 = $this->CI->crud->get_menu('20080227', $username,1);
        $arrMenu2 = $this->CI->crud->get_menu('20080227', $username,2);
        $this->CI->session->set_userdata('menuapp1',$arrMenu0);
        $this->CI->session->set_userdata('menuapp2',$arrMenu1);
        $this->CI->session->set_userdata('menuapp3',$arrMenu2);
    }
		//var_dump($arrMenu0);
    $this->CI->load->vars($data);
  }

  // ------------------------------------------------------------------------
	/**
	 * Create link css/js link on demand
	 */
  function multi_inc($files, $dir=null) {
    $inc_js  = "";
    $inc_css = "";

    if (is_array($files)) {

      foreach ($files as $filename) {
        $inctype = substr(strrchr($filename,"."),1);
        if ($inctype == 'css' || $inctype == '-css') {
          if ($inctype == 'css') {
            $css = $filename;
          } else {
            $css = str_replace(".-css","",$filename);
          }

          $link  = array(
            'href' => $css,
            'rel' => 'stylesheet',
            'type' => 'text/css',
            'media' => 'screen'
          );
          $inc_css .= "\t".link_tag($link);
        }
        elseif ($inctype == 'js') {
          $js  = $filename;
          $inc_js .= "\t".'<script src="' . $js . '" type="text/javascript"></script>'."\n";
        }
      }

    }
    $this->CI->load->vars(array('inc_css'=>$inc_css));
    $this->CI->load->vars(array('inc_js'=>$inc_js));
  }

  // ------------------------------------------------------------------------
	/**
	 * Create inline css/js script on demand
	 */
  function inline_script($lines,$type) {
    $html = '';
    if (is_array($lines) && count($lines)>0 && $type=='js') {
      foreach ($lines as $key => $line) {
        $html .= '<script type="text/javascript">' . "\n" . $line . "\n </script>\n";
      }
    }
    elseif (is_array($lines) && count($lines)>0 && $type=='css') {
      $html .= '<style>'."\n";
      foreach ($lines as $key => $line) {
        $html .= $line."\n";
      }
      $html .= "</style>\n";
    }
    $data['inl_scr'] = $html;
    $this->CI->load->vars($data);
  }

  // ------------------------------------------------------------------------
  /**
   *  Date format
   */
  function format_date($val) {
    $val = str_replace("-","",$val);
    $yr  = substr($val,0,4);
    $mo  = substr($val,4,2);
    $dd  = substr($val,-2);
    $val = date("M d, Y", mktime(0,0,0, $mo, $dd, $yr));
    return $val;
  }

  // ------------------------------------------------------------------------
  /**
   *  Generate random string
   */
  function rand_str($size) {
    $rand_str = '';
    $feed = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for ($i=0; $i < $size; $i++) {
     $rand_str .= substr($feed, rand(0, strlen($feed)-1), 1);
    }
    return $rand_str;
  }

  // ------------------------------------------------------------------------
  /**
   *  nama bulan
   */
  function konv_bulan($mm)
  {
    $mm = intval($mm);
    switch ($mm)
    {
      case 1 : $bulan="Januari";break;
      case 2 : $bulan="Februari";break;
      case 3 : $bulan="Maret";break;
      case 4 : $bulan="April";break;
      case 5 : $bulan="Mei";break;
      case 6 : $bulan="Juni";break;
      case 7 : $bulan="Juli";break;
      case 8 : $bulan="Agustus";break;
      case 9 : $bulan="September"; break;
      case 10 : $bulan="Oktober";break;
      case 11 : $bulan="November";break;
      case 12 : $bulan="Desember";break;
    }
    return $bulan;
  }

  function list_bulan($lang){
      $lbulan = array(array('id' => '1','bulan' => 'Januari'),
                    array('id' => '2','bulan' => 'Februari'),
                    array('id' => '3','bulan' => 'Maret'),
                    array('id' => '4','bulan' => 'April'),
                    array('id' => '5','bulan' => 'Mei'),
                    array('id' => '6','bulan' => 'Juni'),
                    array('id' => '7','bulan' => 'Juli'),
                    array('id' => '8','bulan' => 'Agustus'),
                    array('id' => '9','bulan' => 'September'),
                    array('id' => '10','bulan' => 'Oktober'),
                    array('id' => '11','bulan' => 'November'),
                    array('id' => '12','bulan' => 'Desember')
        );
        return $lbulan;
  }

    function selSearch($current, $name, $id="", $class="", $content)
    {
        $chr   = "s".$current;
        $txt   = "SELECTED ";
        ${$chr}=$txt;
        echo "<select name=\"".$name."\" id=\"".$name."\" class=\"".$class."\">";
        foreach($content as $key=>$value){
          //echo $key . "=>" . $value;
          if($key==$current)
          {
            $txt=" SELECTED ";
          }
          else
          {
            $txt = "";
          }
          echo " <option value=\"$key\"".$txt.">$value</option>";
        }
        echo "</select>";
    }

    function menuAtas($id=null){
        $arrMenu00 = $this->CI->session->userdata('menuapp1');		 
        $arrMenu01 = $this->CI->session->userdata('menuapp2');
        $arrMenu02 = $this->CI->session->userdata('menuapp3');
        $carrMenu00 = count($arrMenu00);
        $carrMenu01 = count($arrMenu01);
        $carrMenu02 = count($arrMenu02);		  
        if(count($arrMenu01)==0){
            $carrMenu01 =1;
        }
        if(count($arrMenu02)==0){
            $carrMenu02 =1;
        }
        $menu = "<table width=\"800\" height=\"110\" background=\"". BASE_URL  ."images/atas.jpg\">";
				$menu .= "	<td>&nbsp;</td>";
        $menu .= "  <td width=\"450\" style=\"vertical-align:bottom\">";
        $menu .= "  <link rel=\"stylesheet\" type=\"text/css\" href=\"".base_url()."/lib/css/superfish.css\" media=\"screen\">";
        $menu .= "  <script type=\"text/javascript\" src=\"".base_url()."/lib/jscript/hoverIntent.js\"></script>";
        $menu .= "  <script type=\"text/javascript\" src=\"".base_url()."/lib/jscript/superfish.js\"></script>";
        $menu .= "  <script type=\"text/javascript\">";
        $menu .= "      jQuery(function(){";
        $menu .= "          jQuery('ul.sf-menu').superfish();";
        $menu .= "          jQuery('ul.sf-menu').children('li').each(function(i){ jQuery(this).css({\"z-index\":30-i}); }); ";
        $menu .= "      });";
        $menu .= "  </script>";
        $menu .= "          <ul  class=\"sf-menu\">";
		$menu .= "          <li><a href=\"/mainpage\">Home</a>";
        foreach($arrMenu00 as $key=>$item)
        {
            $xNomors = substr($item['MNU_NOMORS'],0,4); //Modul Code
            $xOrigin = $item['MNU_NOMORS'];
            $xApplic = $item["MNU_APPLIC"]; //Application Code
            $xDescre = $item["MNU_DESCRE"]; //Application Description (Detail)
            $xNocign = $item["MNU_GOURLS"]; //Nocign = No Codeigniter
			$xNocign = $xNocign=="#" ? "" : "/".$xNocign;
            $menu .="<li><a href=\"$xNocign\">$xDescre</a>";
            
            $ul2 = 1;
            $ulmenu1="";
            foreach($arrMenu01 as $key=>$item2){
                $yApplic = $item2["MNU_APPLIC"];
                $yNomors = substr($item2['MNU_NOMORS'],0,4);
                $yOrigin = $item2['MNU_NOMORS'];
                $yRefers = substr($item2['MNU_REFERS'],0,4);
                $yDescre = $item2["MNU_DESCRE"];
                $yNocign = $item2["MNU_GOURLS"];
                $yAuthrz = $item2["MNU_RIGHTS"];

                if($ul2==1){
                    $ulmenu1 .="<ul>";
                    $menu .= $ulmenu1;
                }
                
                if($xApplic==$yApplic){
                    if($xNomors==$yRefers){
						$yNocign = $yNocign=="#" ? "" : "/".$yNocign;
                        $menu .="<li><a href=\"$yNocign\">$yDescre</a>";
                        $ul3=1;
                        $ulmenu2="";
                        foreach($arrMenu02 as $key=>$item3){
                            $zApplic = $item3["MNU_APPLIC"];
                            $zNomors = substr($item3['MNU_NOMORS'],0,4);
                            $zOrigin = $item3['MNU_NOMORS'];
                            $zRefers = substr($item3['MNU_REFERS'],0,4);
                            $zDescre = $item3["MNU_DESCRE"];
                            $zNocign = $item3["MNU_GOURLS"];
                            $zAuthrz = $item3["MNU_RIGHTS"];
                            if($yApplic==$zApplic){
                                if($yNomors==$zRefers){
                                    if($ul3==1){
                                        $ulmenu2 .="<ul>";
                                        $menu .= $ulmenu2;
                                    }
                                    $menu .="<li><a href=\"/$zNocign\">$zDescre</a>";
                                    $ul3++;
                                }
                            }
                        }
                        if($ulmenu2=="<ul>"){
                            $menu .="</ul>";
                        }
                    }
                }else{
                    //break;
                }
                if($ul2==$carrMenu01){
                    $menu .="</ul>";
                }
                $ul2++;
                
            }
        }
        $menu .="               <li><a href=\"/login/bye\">Log Off</a>";
        $menu .= "          </ul>";
        $menu .= "  </td>";
        $menu .= "</table>";
        return $menu;
    }	
    function menuAtas_ori($id=null){

        $menu = "<table width=\"800\" height=\"145\" background=\"". BASE_URL  ."images/atas.jpg\">";
				$menu .= "	<td>&nbsp;</td>";
        $menu .= "  <td width=\"380\" height=\"145\" style=\"vertical-align:bottom\">";
        $menu .= "  <link rel=\"stylesheet\" type=\"text/css\" href=\"/lib/css/superfish.css\" media=\"screen\">";
        $menu .= "  <script type=\"text/javascript\" src=\"/lib/jscript/hoverIntent.js\"></script>";
        $menu .= "  <script type=\"text/javascript\" src=\"/lib/jscript/superfish.js\"></script>";
        $menu .= "  <script type=\"text/javascript\">";
        $menu .= "      jQuery(function(){";
        $menu .= "          jQuery('ul.sf-menu').superfish();";
        $menu .= "          jQuery('ul.sf-menu').children('li').each(function(i){ jQuery(this).css({\"z-index\":30-i}); }); ";
        $menu .= "      });";
        $menu .= "  </script>";
        $menu .= "          <ul  class=\"sf-menu\">";
        $arrMenu00 = $this->CI->session->userdata('menuapp1');
        $arrMenu01 = $this->CI->session->userdata('menuapp2');
        $arrMenu02 = $this->CI->session->userdata('menuapp3');
        //$arrMenu00 = $this->CI->crud->get_menu('20080227', '9999',0);
        //$arrMenu01 = $this->CI->crud->get_menu('20080227', '9999',1);
        //$arrMenu02 = $this->CI->crud->get_menu('20080227', '9999',2);
				
        $carrMenu00 = count($arrMenu00);
        $carrMenu01 = count($arrMenu01);
        $carrMenu02 = count($arrMenu02);
        if(count($arrMenu01)==0){
            $carrMenu01 =1;
        }
        if(count($arrMenu02)==0){
            $carrMenu02 =1;
        }
        foreach ($arrMenu00 as $key=>$item)
        {

            $x = substr($item['MNU_NOMORS'],0,4);
            $xDescre = $item["MNU_DESCRE"];
            $xLink = $item["MNU_GOURLS"];
            $menu .="<li><a href=\"/$xLink\">$xDescre</a>";
            $ul2 = 1;
            $ulmenu1="";
            foreach($arrMenu01 as $key=>$item2){
                if($ul2==1){
                    $ulmenu1 .="<ul>";
                    $menu .= $ulmenu1;
                }
                $yNomor = substr($item2['MNU_NOMORS'],0,4);
                $y = substr($item2['MNU_REFERS'],0,4);
                $yDescre = $item2["MNU_DESCRE"];
                $yLink   = $item2["MNU_GOURLS"];

                if($x==$y){
                    $menu .="<li><a href=\"/$yLink\">$yDescre</a>";
                    $ul3=1;
                    $ulmenu2="";
                    foreach($arrMenu02 as $key=>$item3){
                        $zNomor = substr($item3['MNU_NOMORS'],0,4);
                        $z = substr($item3['MNU_REFERS'],0,4);
                        $zDescre = $item3["MNU_DESCRE"];
                        $zLink   = $item3["MNU_GOURLS"];
                        if($z==$yNomor){
                            if($ul3==1){
                                $ulmenu2 ="<ul>";
                                $menu .= $ulmenu2;
                            }
                            $menu .="<li><a href=\"/$zLink\">$zDescre</a></li>";
                        }else{
                            $menu .="</li>";
                        }
                        $ul3++;
                    }
										if($ulmenu2=="<ul>"){
												$menu .="</ul>";
										}
                }
                if($ul2==$carrMenu01){
                    $menu .="</ul>";
                }
                $ul2++;
            }
        }
        $menu .="               <li><a href=\"/login/bye\">Log Off</a>";
        $menu .= "          </ul>";
        $menu .= "  </td>";
        $menu .= "</table>";
        return $menu;
    }

    function detailMenu($type, $applic, $nomors){
		$MENUDESC ="";
		$MENUAUTH="";
		$MENUGURL="";
        $arrMenu00 = $this->CI->session->userdata('menuapp1');
        $arrMenu01 = $this->CI->session->userdata('menuapp2');
        $arrMenu02 = $this->CI->session->userdata('menuapp3');
        $carrMenu00 = count($arrMenu00);
        $carrMenu01 = count($arrMenu01);
        $carrMenu02 = count($arrMenu02);
        if(count($arrMenu01)==0){
            $carrMenu01 =1;
        }
        if(count($arrMenu02)==0){
            $carrMenu02 =1;
        }
        foreach ($arrMenu00 as $key=>$item)
        {
            $xNomors =$item['MNU_NOMORS']; //Modul Code
            $xApplic =$item['MNU_APPLIC']; //Modul Code
            if($xNomors==$nomors && $xApplic==$applic){
                $MENUDESC = $item['MNU_DESCRE'];
                $MENUAUTH = $item['MNU_RIGHTS'];
                break;
            }
            foreach($arrMenu01 as $key=>$item2){
                $yNomors =$item2['MNU_NOMORS']; //Modul Code
                $yApplic =$item2['MNU_APPLIC']; //Modul Code
                if($yNomors==$nomors && $yApplic==$applic){
                    $MENUDESC = $item2['MNU_DESCRE'];
                    $MENUAUTH = $item2['MNU_RIGHTS'];
                    break;
                }
                foreach($arrMenu02 as $key=>$item3){
                    $zNomors =$item3['MNU_NOMORS']; //Modul Code
                    $zApplic =$item3['MNU_APPLIC']; //Modul Code
                    if($zNomors==$nomors && $zApplic==$applic){
                        $MENUDESC = $item3['MNU_DESCRE'];
                        $MENUAUTH = $item3['MNU_RIGHTS'];
                        break;
                    }

                }
            }
        }
		
        switch($type){
            case "1" :
                return $MENUDESC;
                break;
            case "2" :
                return $MENUAUTH;
                break;
            case "3" :
                return $MENUGURL;
                break;

        }
    }

 function init_tinymce($param)
  {
    if (!is_array($param)) {
      return false;
    }
	$script[] = '
      tinyMCE_GZ.init({
        plugins : \'save,paste\', themes : \'advanced\', languages : \'en\', disk_cache : true, debug : false
      });
    ';

    foreach ($param as $row)
    {
      list($elements,$width,$height,$full) = $row;
      if ($full) {
        $script[] = '
          tinyMCE.init({
            strict_loading_mode : 1, forced_root_block : \'\', mode : "exact", elements : "'.$elements.'", theme : "advanced", plugins : "save,paste,table", height: "'.$height.'", width: "'.$width.'", theme_advanced_buttons1 : "newdocument,save,|,removeformat,|, " + "pastetext,pasteword,|,bold,italic,underline,fontsizeselect,|," + "bullist,numlist,table,|," + "sub,sup,|,link,unlink,|,charmap,code", theme_advanced_buttons2 : "", theme_advanced_buttons3 : "", theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", theme_advanced_statusbar_location : ""
          });
        ';
      }
      else
      {
        $script[] = '
          tinyMCE.init({
            strict_loading_mode : 1, forced_root_block : \'\', mode : "exact", elements : "'.$elements.'", theme : "advanced", plugins : "save", height: "'.$height.'", width: "'.$width.'", theme_advanced_resizing_min_height : 25, theme_advanced_buttons1 : "newdocument,save", theme_advanced_buttons2 : "", theme_advanced_buttons3 : "", theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", theme_advanced_statusbar_location : ""
          });
        ';
      }
    }
    $this->inline_script($script,'js');

    $data['howto'] = '<div class="howto">
                        <img src="images/admin_icon_info.gif">To create a new line in the editor window, press shift-enter.<br/>
                        To create a new paragraph, press enter.</br>
                      </div>';
    $this->CI->load->vars($data);
  }	
}
/* End of file common.php */
/* Location: ./application/libraries/common.php */
?>